/*
 * mime.cpp
 *
 *  Created on: 2024年9月4日
 *      Author: pengzc
 */

#include "mime.h"
#include "exception.h"
#include "easy.h"

namespace http {

Mime::Mime() {
  curlInit();
  easy_ =  curl_easy_init();
  if (easy_ == NULL) {
    throwHTTPException(-1, "curl_easy_init error");
  }
  mime_ = curl_mime_init(easy_);
  if (mime_ == NULL) {
    curl_easy_cleanup(easy_);
    throwHTTPException(-1, "curl_mime_init error");
  }
}

Mime::~Mime() {
  curl_mime_free(mime_);
  curl_easy_cleanup(easy_);
}

void Mime::add(const std::string& name, const std::string& data) {
  auto part = curl_mime_addpart(mime_);
  curl_mime_data(part, data.data(), data.size());
  curl_mime_name(part, name.c_str());
}

void Mime::addFile(const std::string& name, const std::string& filename,
    const std::string& path) {
  auto part = curl_mime_addpart(mime_);
  curl_mime_filedata(part, path.c_str());
  curl_mime_filename(part, filename.c_str());
  curl_mime_name(part, name.c_str());
}

curl_mime* Mime::mime() const {
  return mime_;
}

} /* namespace http */
